<?php

namespace Sffi\ElementPlus\ElForm;

use Exception;
use Sffi\ElementPlus\entity\Option;
use Sffi\ElementPlus\entityTrait\SelectOption;

class ElFile extends FormItem
{
    protected string $type = 'file';
    protected bool $multiple = false;
    protected string $saveType = 'local';
    protected static array $saveTypes = ['local', 'alioss'];
    protected string $headers;
    protected string $url;
    protected string $method;
    protected string $accept;

    protected static array $fileTypes = [
        'audio' => 'audio/*', 
        'video'=>'video/*', 
        'image'=>'image/*', 
        'office'=>'.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt', 
        'zip'=>'.zip,.rar,.7z,.tar.gz'
    ];

    public function __construct($title, $key, $isList=false)
    {
        $this->title = $title;
        $this->key = $key;
        $this->prop = $key;
        $this->multiple = $isList;
    }

    /**
     * 设置文件类型
     * @param string $fileType enum: audio, video, image, office, zip
     */
    public function setFileType($fileType = "image"){
        if(key_exists($fileType, self::$fileTypes)){
            $this->accept = self::$fileTypes[$fileType];
            return $this;
        }
        throw new Exception('错误的文件类型', 1);
    }

    /**
     * 设置保存方式
     * @param string $saveType local 本地  alioss 阿里云
     * @return $this
     */
    public function setSaveType(string $saveType = 'local', $localUrl = ''){
        if  (in_array($saveType, self::$saveTypes)) {
            $this->saveType = $saveType;
            $this->url = $localUrl;
            return $this;
        }
        throw new \Exception('保存方式错误');
    }

    /**
     * 设置是否多选
     * @param bool $multiple
     * @return $this
     */
    public function setMultiple(bool $multiple): self
    {
        $this->multiple = $multiple;
        return $this;
    }

    /**
     * 设置上传请求表头
     * @param array $headers
     * @return $this
     */
    public function setHeaders($headers = []) {
        $this->headers = json_encode($headers);
        return $this;
    }
}
